package com.hiray.demos.android_features

import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.appcompat.app.AppCompatActivity
import com.hiray.demos.ItemAdapter
import com.hiray.demos.R
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import kotlinx.android.synthetic.main.activity_android_feature.*

class AndroidFeatureActivity : AppCompatActivity(), ItemAdapter.OnItemClickListener {

    val GAME_CHANNEL_ID = "314159";
    val FRIEDN_CHANNEL_ID = "314160";
    val CHANNEL_FRIEND = "friend";
    val CHANNEL_GAME = "game";

    val items = arrayListOf(
            "Android 8.0 通知渠道(NotificationChannel)",
            "应用屏保(点击后熄屏启用)"
    )


    @RequiresApi(Build.VERSION_CODES.O)
    override fun onItemClick(position: Int) {
        overridePendingTransition(0, 0)
        when (position) {
            0 -> createAndSendNotification()
            1 -> screenLock()
        }
    }

    private fun screenLock() {

    }

    /**
     * Android O 通知渠道
     */
    @RequiresApi(Build.VERSION_CODES.O)
    private fun createAndSendNotification() {
        //IMPORTANCE_HIGH 的通知会全局弹窗展示
        val gameNc = NotificationChannel(GAME_CHANNEL_ID, CHANNEL_GAME, NotificationManager.IMPORTANCE_HIGH)
        val friendNc = NotificationChannel(FRIEDN_CHANNEL_ID, CHANNEL_FRIEND, NotificationManager.IMPORTANCE_DEFAULT)
        val notificationManager = getSystemService(NotificationManager::class.java) as NotificationManager
        notificationManager.createNotificationChannel(gameNc)
        notificationManager.createNotificationChannel(friendNc)
        val gameNt = NotificationCompat.Builder(this, GAME_CHANNEL_ID)
                .setTicker(resources.getString(R.string.game_ticker))
                .setContentText("hello game buddy!")
                .setSubText("hello mew game buddy message!")
                .setSmallIcon(R.mipmap.game).build()
        val friendNt = NotificationCompat.Builder(this, FRIEDN_CHANNEL_ID)
                .setTicker(resources.getString(R.string.friend_ticker))
                .setContentText("hello buddy!")
                .setSubText("hello mew buddy message!")
                .setSmallIcon(R.mipmap.wechat_message)
                .build()
        notificationManager.notify("gameNt", 0, gameNt)
        notificationManager.notify("friendNt", 1, friendNt)


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_android_feature)

        val itemAdapter = ItemAdapter(items, this)
        itemAdapter.cornerMarker = true
        recyclerview.adapter = itemAdapter
        recyclerview.addItemDecoration(HorizontalDividerItemDecoration.Builder(this@AndroidFeatureActivity)
                .colorResId(R.color.divider_light)
                .size(5)
                .build())
    }

}
